// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false
import axios from 'axios'

//加拦截器，每前请求前头部加上token
axios.interceptors.request.use(config => {
    var token = localStorage.getItem("token")
    if (token) {
        config.headers["token"] = token
    }
    return config
})

Vue.prototype.axios = axios;
axios.defaults.baseURL = "http://127.0.0.1:8000/"


import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';



Vue.use(ElementUI);

router.beforeEach((to, from, next) => {
    //定义白名单
    var white = ['/login', '/modifypwd', '/', '/h']
        //判断是否在白名单中，如果在继续执行next()
    if (white.indexOf(to.path) >= 0) {
        next()
    } else {
        //如果不在判断token是否存在，如果token存在已经登录
        var token = localStorage.getItem("token")
        if (token) {
            //判断用户表中mpstatus状态如果为1，只能跳转到修改密码页面
            var mpstatus = localStorage.getItem("mpstatus")
            if (mpstatus == 2) {
                window.location.href = "http://localhost:8080/#/modifypwd"
            } else {
                //判断此用户访问的url是否在允许的menu内
                var pomitionlist = JSON.parse(localStorage.getItem("pomitionlist"))
                if (pomitionlist.indexOf(to.path) >= 0) {
                    next()
                } else {
                    alert("您没有权限访问此页面")
                    return false;
                }
            }
        } else {
            alert("无权访问")
            window.location.href = "http://localhost:8080/#/login"
            return;
        }

    }


})








// router.beforeEach((to, from, next) => {
//     var white = ['/login', "/"]
//     alert(to.path)
//     if (white.indexOf(to.path) >= 0) {
//         next();
//     } else {
//         var token = localStorage.getItem("token")
//         var t1 = 1
//         token = '123'
//         if (token) {
//             if (t1 == 1) {
//                 next()
//             } else {
//                 window.location.href = "http://www.baidu.com"
//             }
//         } else {
//             alert("无权访问")
//             return false;
//         }
//     }
// })

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
})